// ==UserScript==
// @name          Bruinzone friend/foe
// @namespace     Bruinzone.com
// @description   hide/show friend/foe on bruinzone.com
// @include       http://*.bruinzone.com/*
// ==/UserScript==

(function(){


	setIgnoreList = function()
	{
		var str = prompt("Enter names to put on your ignore list (separate multiple names by comma (,):", GM_getValue("LIST_IGNORE",""));

		GM_setValue("LIST_IGNORE", str.trim());
	};

	setFoeList= function()
	{
		var str = prompt("Enter names who should be in your foe list (separate multiple names by comma (,):", GM_getValue("LIST_FOE",""));
		GM_setValue("LIST_FOE", str.trim());
	}

	setFriendList = function()
	{
		var str = prompt("Enter names you want to be on your friend list (separate multiple names by comma (,):", GM_getValue("LIST_FRIEND",""));
		GM_setValue("LIST_FRIEND", str.trim());
	}

	toggleDisplayIgnore = function()
	{
		GM_setValue("SHOW_IGNORE", !GM_getValue("SHOW_IGNORE",true));
	}

	GM_registerMenuCommand("Set users to your ignore list", setIgnoreList);
	GM_registerMenuCommand("Set users to your foe list", setFoeList);
	GM_registerMenuCommand("Set users to your friend list", setFriendList);
	GM_registerMenuCommand("Toggle displaying of ignored posts", toggleDisplayIgnore);


	// define some highlighted colors.  These are customizable.

	strTrojieColor = "#F87CA7";

	strBruinColor = "#FFFF33";


	var allLinks, thisLink;

	allLinks = document.evaluate(
	    getXPathQuery(splitList(GM_getValue("LIST_IGNORE",""))),
	    document,
	    null,
	    XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,
	    null);

	for (var i = 0; i < allLinks.snapshotLength; i++) {
	    thisLink = allLinks.snapshotItem(i);

		try{
			hideThem(thisLink);
		}
		catch(e)
		{
		}
	}

	// get foe posters
	allLinks = document.evaluate(
	    getXPathQuery(splitList(GM_getValue("LIST_FOE",""))),
	    document,
	    null,
	    XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,
	    null);

	for (var i = 0; i < allLinks.snapshotLength; i++) {
	    thisLink = allLinks.snapshotItem(i);

		showFriendFoe(thisLink, strTrojieColor,"normal");

	}


	// get friend posters
	allLinks = document.evaluate(
	    getXPathQuery(splitList(GM_getValue("LIST_FRIEND",""))),
	    document,
	    null,
	    XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,
	    null);

	for (var i = 0; i < allLinks.snapshotLength; i++) {
	    thisLink = allLinks.snapshotItem(i);

		showFriendFoe(thisLink, strBruinColor,"bold");

	}



	function hideThem(objElement) {
		if(objElement.parentNode)
		{
			for(var i=0;i<objElement.parentNode.childNodes.length;i++)
			{
				var el = objElement.parentNode.childNodes[i];
				if(el.nodeName != "UL" &&  el.nodeName != "CENTER" && el.nodeName != "HR" && el.nodeName != "BR")
				{
					if(objElement.parentNode.childNodes[i].nodeType != 1)
					{
						objElement.parentNode.childNodes[i].textContent = "";
					}
					else {
						objElement.parentNode.childNodes[i].style.display = "none";
					}
				}
			}
			if(GM_getValue("SHOW_IGNORE",true)) {
				newElement = document.createElement('i');
				newElement.innerHTML = "<strong>" + objElement.textContent + "</strong> is on your ignore list";
				objElement.parentNode.insertBefore(newElement, objElement);
			}
			else {
				newElement = document.createElement('br');
				objElement.parentNode.insertBefore(newElement, objElement);
			}

		}
		return;
	}

	function showFriendFoe(objElement, colorCode, linkWeight) {
		objElement.style.color = colorCode;
		objElement.parentNode.firstChild.style.color = colorCode;
		objElement.parentNode.firstChild.style.fontWeight = linkWeight;
	}

	String.prototype.ltrim = function() {
	    return this.replace(/^\s+/g, '');
	}
	String.prototype.rtrim = function() {
	    return this.replace(/\s+$/g, '');
	}
	String.prototype.trim = function() {
	    return this.replace(/^\s+|\s+$/g, '');
  	}

	function splitList(str)
	{
		var arr = str.split(",");

		for(i=0;i<arr.length;i++)
		{
			arr[i] = arr[i].trim();
		}
		return arr;

	}

	function getXPathQuery(myArray)
	{
		startXpathQuery = "//li/b[translate(normalize-space(.),'ABCDEFGHIJKLMNOPQRSTUVWXYZ','abcdefghijklmnopqrstuvwxyz')='xxyyzznameholderxxyyzz']";
		resultXpathQuery = "";

		for(var i=0; i < myArray.length;i++)
		{
			resultXpathQuery = resultXpathQuery + startXpathQuery.replace(/xxyyzznameholderxxyyzz/,myArray[i].toLowerCase());
			resultXpathQuery = resultXpathQuery + " | ";
		}

		return resultXpathQuery.substr(0, resultXpathQuery.length - 2);

	}

})();
